﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Hockey.Shared
{
    /// <summary>
    /// General Service Interface
    /// </summary>
    public interface IService
    {
    }

    /// <summary>
    /// Service class for registering services
    /// </summary>
    public static class ServiceLocator
    {
        /// <summary>
        /// Map of services
        /// </summary>
        private static Dictionary<Type, IService> mServices;

        /// <summary>
        /// Static constructor
        /// </summary>
        static ServiceLocator()
        {
            mServices = new Dictionary<Type,IService>();
        }

        /// <summary>
        /// Registers a new instance of a service
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        public static void RegisterService(Type type, IService instance) 
        {
            mServices[type] = instance;
        }

        /// <summary>
        /// Returns a service by interface
        /// </summary>
        public static IService GetService(Type type) 
        {
            try
            {
                IService instance = mServices[type];
                return instance;
            }
            catch
            {
                throw new Exception("Unknown service.");
            }           
        }
    }
}
